package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;

    @Override
    public Map<String, Object> getCustomerList(Integer page, Integer rows, String customerName) {
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,rows);
        Page<Customer> customerPage = customerDao.selectCustomer(customerName);
        map.put("total",customerPage.getTotal());
        map.put("rows",customerPage.getResult());
        return map;
    }

    @Override
    public void saveCustomer(Customer customer) {
        Integer customerId = customer.getCustomerId();
        if (customerId==null){
            customerDao.insertCustomer(customer);
        }else {
            customerDao.updateCustomer(customer);
        }
    }

    @Override
    public void deleteCustomer(String[] ids) {
        for (String id : ids) {
            customerDao.deleteCustomer(id);
        }
    }
}
